'''
Created on 2018年9月11日

@author: lijie
'''
import os

from utilites.database_helper import MysqlHelper
from utilites.rmhelper import RunManageHelper

mydb=MysqlHelper('ev1','rm')
module="api_userinfo"
pro_name="小强科技"
m_id=mydb.query("SELECT id FROM module WHERE mName='"+module+"' ")
p_id=mydb.query("SELECT id FROM project WHERE proName='"+pro_name+"' ")
if len(m_id)>0 and len(p_id)>0:
    m_id=m_id[0]['id']
    p_id=p_id[0]['id']
    path=os.path.dirname(__file__)+'/'+module
    cases=RunManageHelper().get_pyList(path)
    new_cases=cases.copy()
    try:
        for case in cases:
            li=mydb.query("select * from cases where caseName='"+case+"' and pid ='"+str(p_id)+"'")
            if len(li)>0: 
                new_cases.remove(case)              #移除已经在库里存在的case名
                print(case+' was added before!')
        if len(new_cases)>0:
            for case in new_cases:            
                mydb.excute("insert into cases value(NULL,'"+case+"','"+str(m_id)+"','"+str(p_id)+"')")
                print("case: "+case+" had add to db")
        print("add case completed!!") 
    finally:
        mydb.close_conn()
else:
    mydb.close_conn()
    print("not found current module or project!!pls check the input.")  
        
if __name__ == '__main__':
    pass
    